<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'Addsubject.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <%
		request.setCharacterEncoding("UTF-8");
		String subjectname = request.getParameter("subjectname");
		int classhour = Integer.parseInt(request.getParameter("classhour"));
		int gradeid = Integer.parseInt(request.getParameter("gradeid"));
	%>
	<%
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost:3306/mschool?useSSL=false";
			conn = DriverManager.getConnection(url, "root", "123456");
			String sql = "INSERT INTO subject(subjectname,classhour,gradeid) VALUES(?,?,?);";
			ps = conn.prepareStatement(sql);
			ps.setString(1, subjectname);
			ps.setInt(2, classhour);
			ps.setInt(3, gradeid);
			int result = ps.executeUpdate();
			out.print("<div align='center' style='color: blue;font-size: 50px;'>成功添加" + result + "行</div");
		} catch (Exception e) {
			out.print("<div align='center' style='color: red;font-size: 50px;font-weight: bold;'>添加失败!</div>");
			e.printStackTrace();
		} finally {
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	%>
  </body>
</html>
